package com.styytech.dadabao.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.AnimationDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.styytech.dadabao.R;

public class BaseFragment extends Fragment {

    private View rootView;
    protected Context context;
    protected Activity mActivity;


    /**
     * 加载框
     */
    protected LinearLayout ll_loginInformation;
    /**
     * 加载中图标
     */
    protected ImageView pb_loadProgressBar;
    /**
     * 提示语
     */
    protected TextView tv_infor;
    /**
     * 重试
     */
    protected ImageView iv_reloadBtn;

    protected Activity getmActivity() {
        return mActivity;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {// 在前面执行

        super.onCreate(savedInstanceState);
        context = getActivity();
        // 获取参数
        Bundle bundle = getArguments();
        if (null != bundle) {

        }
        setNetReceiver();
    }

    public void initLoadingView(View rootView) {

        ll_loginInformation = (LinearLayout) rootView
                .findViewById(R.id.ll_loginInformation);
        pb_loadProgressBar = (ImageView) rootView
                .findViewById(R.id.pb_loadProgressBar);
        tv_infor = (TextView) rootView.findViewById(R.id.tv_infor);
        iv_reloadBtn = (ImageView) rootView
                .findViewById(R.id.iv_reloadBtn);
        if (iv_reloadBtn != null) {
            iv_reloadBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    reloadBtnOnclick();
                }
            });
        }
        if (pb_loadProgressBar != null) {
            AnimationDrawable anim = (AnimationDrawable) pb_loadProgressBar
                    .getBackground();
            if (anim != null) {
                anim.start();
            }
        }
    }

    protected void reloadBtnOnclick() {

    }

    /**
     * 设置提示信息
     *
     * @param visiLinearLayout 总体布局是否可见
     * @param visiProgressBar  加载框是否可见
     * @param visiTextView     提示信息组件
     * @param info             提示信息
     */
    public void setView(int visiLinearLayout, int visiProgressBar,
                        int visiTextView, int visiImageButn, String info) {
        if (ll_loginInformation != null)
            ll_loginInformation.setVisibility(visiLinearLayout);
        if (pb_loadProgressBar != null)
            pb_loadProgressBar.setVisibility(visiProgressBar);
        if (tv_infor != null)
            tv_infor.setVisibility(visiTextView);
        if (iv_reloadBtn != null)
            iv_reloadBtn.setVisibility(visiImageButn);
        if (tv_infor != null && info != null) {
            tv_infor.setText(info);
        }
    }

    private void setNetReceiver() {
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        getActivity().registerReceiver(mReceiver, mFilter);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState, int layoutId) {
        super.onCreateView(inflater, container, savedInstanceState);

        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(layoutId, null);
            ViewUtils.inject(this, rootView); // 注入view和事件
            initLoadingView(rootView);
        }

        return rootView;
    }

    @Override
    public void onDestroy() {
        try {
            if (mReceiver != null) {
                getActivity().unregisterReceiver(mReceiver);
            }
        } catch (IllegalArgumentException e) {
        }

        super.onDestroy();
    }

    /**
     * 网络状态监测
     */
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager;
            NetworkInfo info = null;
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                connectivityManager = (ConnectivityManager) getActivity()
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                info = connectivityManager.getActiveNetworkInfo();
                if (info != null && info.isAvailable()) {
                    connectTivityService();
                } else {
                    connectTivityErroe();
                }
            }
        }
    };

    /**
     * 网络连接正常
     */
    protected void connectTivityService() {

    }

    /**
     * 网络连接异常
     */
    protected void connectTivityErroe() {

    }
}
